if ( SERVER ) then

	AddCSLuaFile( "shared.lua" )
	
	SWEP.Weight = 5
	SWEP.AutoSwitchTo = false
	SWEP.AutoSwitchFrom = false 
	
	SWEP.HoldType = "smg"
	
	// Weapon icon.
	resource.AddFile( "materials/vgui/entities/rt_combinemortar.vtf" )
	resource.AddFile( "materials/vgui/entities/rt_combinemortar.vmt" )
	
elseif ( CLIENT ) then

	SWEP.PrintName = "Combine Mortar"
	SWEP.Slot = 4
	SWEP.SlotPos = 4
	SWEP.DrawAmmo = false
	SWEP.DrawCrosshair = true
	
end

SWEP.Author					= "RabidToaster"
SWEP.Contact				= ""
SWEP.Purpose				= ""
SWEP.Instructions			= ""

SWEP.Spawnable				= false
SWEP.AdminSpawnable			= true

SWEP.ViewModel				= "models/weapons/v_irifle.mdl"
SWEP.WorldModel				= "models/weapons/w_irifle.mdl"
SWEP.ViewModelFlip 			= false

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= true
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"

SWEP.Primary.Delay			= 1.5
SWEP.Primary.Damage			= 100
SWEP.MaxOffFlat = 80 // Maximum degrees off of flat to be.


SWEP.LastFire = 0

function SWEP:Precache()
	util.PrecacheSound( "Weapon_Mortar.Single" )
	util.PrecacheSound( "Weapon_Mortar.Incomming" )
end

function SWEP:PrimaryAttack()
	// Find where the player is aiming.
	local targetTrace = util.QuickTrace( self.Owner:GetShootPos(), self.Owner:GetAimVector() * 8192, self.Owner )
	
	// If we hit the sky/walls/ceilings, don't bother.
	if ( targetTrace.HitSky ) then return end
	if ( math.deg( math.acos( targetTrace.HitNormal:Dot( Vector( 0, 0, 1 ) ) ) ) > self.MaxOffFlat ) then return end
	
	// Trace up to find the sky.
	local skyTrace = util.TraceLine( { start = targetTrace.HitPos, endpos = targetTrace.HitPos + Vector( 0, 0, 16383 ), filter = self.Owner, mask = MASK_NPCWORLDSTATIC } )
	if ( !skyTrace.HitSky ) then return end
	// Prevents rare crash when mortar is below the target.
	if ( skyTrace.HitPos.z <= targetTrace.HitPos.z ) then return end
	
	// Effects.
	self:EmitSound( "Weapon_Mortar.Single" )
	self:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
	self.Owner:ViewPunch( Angle( -0.1, 0, 0 ) )
	
	// Timing.
	self:SetLastFire()
	self:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	
	if ( !SERVER ) then return end
	
	// Create the mortar.
	local mortar = ents.Create( "func_tankmortar" )	
		mortar:SetPos( skyTrace.HitPos )
		mortar:SetAngles( Angle( 90, 0, 0 ) )
		mortar:SetKeyValue( "iMagnitude", self.Primary.Damage ) // Damage.
		mortar:SetKeyValue( "firedelay", "1" ) // Time before hitting.
		mortar:SetKeyValue( "warningtime", "1" ) // Time to play incoming sound before hitting.
		mortar:SetKeyValue( "incomingsound", "Weapon_Mortar.Incomming" ) // Incoming sound.
	mortar:Spawn()
	
	// Create the target.
	local target = ents.Create( "info_target" )
		target:SetPos( targetTrace.HitPos )
		target:SetName( tostring( target ) )
	target:Spawn()
	mortar:DeleteOnRemove( target )
	
	// Fire.
	mortar:Fire( "SetTargetEntity", target:GetName(), 0 )
	mortar:Fire( "Activate", "", 0 )
	mortar:Fire( "FireAtWill", "", 0 )
	mortar:Fire( "Deactivate", "", 2 )
	mortar:Fire( "kill", "", 2 )
	
end

// Used for the laser effect.
function SWEP:SetLastFire()
	self.LastFire = CurTime()
	if ( SERVER && SinglePlayer() ) then
		self:CallOnClient( "SetLastFire", "" )
	end
end

function SWEP:SecondaryAttack()
end

local laser
if ( CLIENT ) then
	laser = Material( "sprites/bluelaser1" )
end
function SWEP:ViewModelDrawn()
	// Get the muzzle position, where we will start the beam.
	local muzzle = self:Muzzle()
	local muzzlePos = muzzle.Pos + ( muzzle.Ang:Forward() * 12 )
	// Find where the player is looking.
	local targetPos = util.QuickTrace( self.Owner:GetShootPos(), self.Owner:GetAimVector() * 8192, self.Owner ).HitPos
	
	// If we've just fired, change the beams colour from red to light blue.
	local colour = Color( 255, 0, 0 )
	if ( CurTime() < self.LastFire + 0.1 ) then
		colour = Color( 0, 255, 255 )
	end
	
	// Draw the beam from muzzle to target.
	render.SetMaterial( laser )
	render.DrawBeam( muzzlePos, targetPos, 2, 0, 64, colour )
end

// Gets the viewmodel's muzzle attachment.
function SWEP:Muzzle()
	local model = self.Owner:GetViewModel()
	local muzzle = model:LookupAttachment( "muzzle" )
	if ( muzzle == 0 ) then muzzle = model:LookupAttachment( "1" ) end
	local muzzleVals = model:GetAttachment( muzzle )
	return muzzleVals
end
